import React, {useEffect, useRef, useState} from "react";
import {AddCollege, getBuRoom, getClasses, getCrm, updateCollege} from "../../../../../api/api.js";
import {Button, Col, Form, Modal, Row, Toast} from "@douyinfe/semi-ui";
import {IconEdit, IconPlus, IconUserAdd} from "@douyinfe/semi-icons";

function College_EForm(props) {
    const [visible, setVisible] = useState(false);
    const [treeData,settreeData] = useState(null)
    const [list3,setlist3] = useState(0)
    const api = useRef(null);
    useEffect(()=>{
        if (visible) {
            setTimeout(() => {
                if (api.current) {
                    api.current.setValue("collegename", props.mess.cname);
                }
            }, 0);
        }
    },[visible])
    async function sub() {
        let form = new FormData()
        form.append("name",api.current.getValues().collegename)
        form.append("id",props.mess.cid)
        const res = await updateCollege(form);
        if (res.data.code==200){
            Toast.success('学院修改成功')
            handleOk()
        }else {
            Toast.success('学院修改失败')
            handleOk()
        }
    }
    function valte() {
        api.current.validate()
            .then(values=>{
                sub()
            })
            .catch(errors=>{
                Toast.error("请重新填写表单")
            })
    }

    const showDialog = () => {
        setVisible(true);
    };
    const handleOk = () => {
        setVisible(false);
        console.log('Ok button clicked');
    };
    const handleCancel = () => {
        setVisible(false);
        console.log('Cancel button clicked');
    };
    const handleAfterClose = () => {
        console.log('After Close callback executed');
    };


    return (
        <>
            <Button onClick={showDialog} type='secondary'  icon={<IconEdit />} theme="solid" style={{ marginRight: 10 }}>修改信息</Button>
            <Modal
                okText={'Sounds great!'}
                style={{width:"30%",height:"30%"}}
                title={<div><IconEdit  style={{marginRight:"10px"}} />修改信息</div>}
                visible={visible}
                onOk={handleOk}
                afterClose={handleAfterClose} //>=1.16.0
                onCancel={handleCancel}
                closeOnEsc={true}
                footer={
                    <div style={{textAlign:'center'}}>
                        <Button type="primary" onClick={()=>{valte()}}>
                            提交
                        </Button>
                    </div>

                }
            >
                <Row style={{display: 'flex',justifyContent:'center'}}>
                    <Form
                        getFormApi={formApi => api.current = formApi}
                        onSubmit={values => Toast.info({content: JSON.stringify(values)})}
                    >
                        <>

                            <Form.Input
                                rules={[
                                    { required: true, message: '' },
                                    { type: 'string', message: 'type error' },
                                    { validator: (rule, value) => value != null, message: '学院名不能为空' },
                                ]}
                                style={{justifyContent:'center'}} label={'学院名'} field='collegename'
                                        placeholder='请输入学院名'/>
                        </>

                    </Form>
                </Row>
            </Modal>
        </>
    );
}
export default College_EForm